'''

双集群gs_ddr相关公共函数

'''

from yat.test import macro

from testcase.utils.Logger import Logger

class DC_CommonSH:
    def __init__(self):
        self.log = Logger()

    def gs_ddr_start(self, node, env='', mode='', config='', disaster_type='', param=''):
        cmd = f'''source {env};
                gs_ddr -t start -m {mode} {config} {disaster_type} {param}'''
        self.log.info(cmd)
        res = node.sh(cmd).result()
        return res

    def gs_ddr_stop(self, node, env='', config='', tmp='', param=''):
        if not tmp:
            self.log.info('tmp is null')
            return '临时目录tmp参数值不能为空'
        check_cmd = f'''
                    if [ ! -d "{tmp}" ]; then
                        echo "ERROR: 临时目录 {tmp} 不存在"
                        exit 1
                    fi
                    if [ ! -w "{tmp}" ]; then
                        echo "ERROR: 对临时目录 {tmp} 没有写入权限"
                        exit 1
                    fi'''
        check_result = node.sh(check_cmd).result()
        if check_result and "ERROR" in check_result:
            self.log.error(f"临时目录检查失败: {check_result}")
            raise PermissionError(f"临时目录检查失败: {check_result}")
    
        cmd = f'''rm -rf {tmp}/.ddr_stop.step;
                source {env};
                gs_ddr -t stop {config} {param}'''
        self.log.info(cmd)
        res = node.sh(cmd).result()
        return res

    def gs_ddr_failover(self, node, env='', tmp='', param=''):
        if not tmp:
            self.log.info('tmp is null')
            return '临时目录tmp参数值不能为空'
        check_cmd = f'''
                    if [ ! -d "{tmp}" ]; then
                        echo "ERROR: 临时目录 {tmp} 不存在"
                        exit 1
                    fi
                    if [ ! -w "{tmp}" ]; then
                        echo "ERROR: 对临时目录 {tmp} 没有写入权限"
                        exit 1
                    fi'''
        check_result = node.sh(check_cmd).result()
        if check_result and "ERROR" in check_result:
            self.log.error(f"临时目录检查失败: {check_result}")
            raise PermissionError(f"临时目录检查失败: {check_result}")
        cmd = f'''rm -rf {tmp}/.ddr_failover.step;
                source {env};
                gs_ddr -t failover {param}'''
        self.log.info(cmd)
        res = node.sh(cmd).result()
        return res

    def gs_ddr_switchover(self, node, env='', mode='', tmp='', param=''):
        if not tmp:
            self.log.info('tmp is null')
            return '临时目录tmp参数值不能为空'
        check_cmd = f'''
                    if [ ! -d "{tmp}" ]; then
                        echo "ERROR: 临时目录 {tmp} 不存在"
                        exit 1
                    fi
                    if [ ! -w "{tmp}" ]; then
                        echo "ERROR: 对临时目录 {tmp} 没有写入权限"
                        exit 1
                    fi'''
        check_result = node.sh(check_cmd).result()
        if check_result and "ERROR" in check_result:
            self.log.error(f"临时目录检查失败: {check_result}")
            raise PermissionError(f"临时目录检查失败: {check_result}")
        cmd = f'''rm -rf {tmp}/.ddr_switchover_primary.step;
                rm -rf {tmp}/.ddr_switchover_standby.step;
                source {env};
                gs_ddr -t switchover -m {mode} {param}'''
        self.log.info(cmd)
        res = node.sh(cmd).result()
        return res

    def gs_ddr_query(self, node, env='', tmp='', param=''):
        if not tmp:
            self.log.info('tmp is null')
            return '临时目录tmp参数值不能为空'
        check_cmd = f'''
                    if [ ! -d "{tmp}" ]; then
                        echo "ERROR: 临时目录 {tmp} 不存在"
                        exit 1
                    fi
                    if [ ! -w "{tmp}" ]; then
                        echo "ERROR: 对临时目录 {tmp} 没有写入权限"
                        exit 1
                    fi'''
        check_result = node.sh(check_cmd).result()
        if check_result and "ERROR" in check_result:
            self.log.error(f"临时目录检查失败: {check_result}")
            raise PermissionError(f"临时目录检查失败: {check_result}")
        cmd = f'''rm -rf {tmp}/.ddr_query.step;
                source {env};
                gs_ddr -t query {param}'''
        self.log.info(cmd)
        res = node.sh(cmd).result()
        return res